package com.aaa.service;

import com.aaa.dao.EmpDao;
import com.aaa.entity.Emp;
import com.aaa.unilt.JwtUntil;
import com.aaa.vo.EmpSearchForm;
import com.aaa.vo.LoginVo;
import com.aaa.vo.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author C-Q
 * @version 1.1.0
 * @Description: TODO
 * @date 2022/9/28
 */
@Service
public class EmpDaoServiceImpl implements EmpDaoService{
    @Autowired
    private EmpDao empDao;

    @Override
    public PageInfo<Emp> findAll(int currentPage, int pageSize, EmpSearchForm empSearchForm) {
        PageHelper.startPage(currentPage,pageSize);
        System.out.println("empSearchForm Service= " + empSearchForm);
        if(empSearchForm.getDate()!=null){
            empSearchForm.setStartDate(empSearchForm.getDate()[0]);
            empSearchForm.setEndDate(empSearchForm.getDate()[1]);
        }
        List<Emp> emps = empDao.findAll(empSearchForm);
        //把查询的结果封装到pageInfo对象
        PageInfo<Emp> pageInfo = new PageInfo<>(emps);
        return pageInfo;
    }
    /*登录验证*/
    @Override
    public Result findByNameAge(LoginVo loginVo) {
        Emp byNameAge = empDao.findByNameAge(loginVo);
        if(loginVo.getUsername().equals(byNameAge.getName())&&loginVo.getPassword().equals(byNameAge.getAge())){
            //生成
            Map<String,Object> map=new HashMap<>();
            map.put("name",loginVo.getUsername());
            map.put("age",loginVo.getPassword());
            String token = JwtUntil.createToken(map);
            return new Result(2000,"登录成功",token);
        }
        return new Result(5000,"登录失败",null);
    }


    @Override
    public int insert(Emp record) {
        int insert01 = empDao.insert(record);
        return insert01;
    }

    @Override
    public int updateByPrimaryKey(Emp record) {
        int i = empDao.updateByPrimaryKey(record);
        return i;
    }


    @Override
    public int deleteByPrimaryKey(Integer id) {
        return empDao.deleteByPrimaryKey(id);
    }

    @Override
    public int insertSelective(Emp record) {
        return 0;
    }
    @Override
    public Emp selectByPrimaryKey(Integer id) {
        return null;
    }
    @Override
    public int updateByPrimaryKeySelective(Emp record) {
        return 0;
    }


}
